package com.kdgz.kdgz.util;

import com.kdgz.kdgz.beans.Area;
import com.kdgz.kdgz.beans.Orders;
import com.kdgz.kdgz.beans.Rule;
import com.kdgz.kdgz.sevice.AreaService;
import com.kdgz.kdgz.sevice.OrdersService;
import com.kdgz.kdgz.sevice.RuleService;
import com.kdgz.kdgz.sevice.impl.AreaServiceImpl;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Desc 生成随机的订单数据
 * @date 2021/3/23
 */
@Component
@NoArgsConstructor
public class RandomOrders {
       @Autowired
       private  AreaService areaService;

       @Autowired
       private OrdersService ordersService;
       @Autowired
       private RuleService ruleService;
       private  List<Area> allArea = null;
    String familyName = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻水云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳鲍史唐费岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅卞齐康伍余元卜顾孟平"
            + "黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计成戴宋茅庞熊纪舒屈项祝董粱杜阮席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田胡凌霍万柯卢莫房缪干解应宗丁宣邓郁单杭洪包诸左石崔吉"
            + "龚程邢滑裴陆荣翁荀羊甄家封芮储靳邴松井富乌焦巴弓牧隗山谷车侯伊宁仇祖武符刘景詹束龙叶幸司韶黎乔苍双闻莘劳逄姬冉宰桂牛寿通边燕冀尚农温庄晏瞿茹习鱼容向古戈终居衡步都耿满弘国文东殴沃曾关红游盖益桓公晋楚闫";
    String firstName2 = "欧阳太史端木上官司马东方独孤南宫万俟闻人夏侯诸葛尉迟公羊赫连澹台皇甫宗政濮阳公冶太叔申屠公孙慕容仲孙钟离长孙宇文司徒鲜于司空闾丘子车亓官司寇巫马公西颛孙壤驷公良漆雕乐正宰父谷梁拓跋夹谷轩辕令狐段干百里呼延东郭南门羊舌微生公户公玉公仪梁丘公仲公上公门公山公坚左丘公伯西门公祖第五公乘贯丘公皙南荣东里东宫仲长子书子桑即墨达奚褚师吴铭";
    String girlName = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽";
    String boyName = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";
    String product[] = {"水果", "果蔬", "数码", "易碎物品", "文具", "海产", "双肩背包", "螺蛳粉", "男鞋", "女鞋", "服装", "充电宝", "手机", "耳机", "乐高", "牙膏"};

    /**
     * 随机生成订单信息
     * @return com.kdgz.kdgz.beans.Orders
     */
    public Orders randInfo() {
        allArea = areaService.getAllArea();
        int random = 0;
        do {
         random = new Random().nextInt(allArea.size());
        }while (random == 0);
        int aid = allArea.get(random).getAid();
        Orders order = new Orders();
        // 随机生成配送区域
        order.setAid(aid);
        // 随机收件人姓名
        String familyName = randFamilyName();
        String name = randName(randSex());
        String username = familyName.concat(name);
        order.setUsername(username);
        // 随机生成电话号码
        order.setPhone(getPhoneNum());
        // 随机生成物品
        order.setProduct(getProduct());
        // 随机生成质量
        int weight = new Random().nextInt(100)+1;
        order.setWeight(new BigDecimal(weight));
        // 随机生成价格
        order.setPrice(getPrice(weight));
        // 随机生成时间
        order.setDate(getDate());

        // 随机生成问题件（1%的概率）
        int i = new Random().nextInt(100) + 1;
        if (i ==44)
            order.setStatus(3);
        ordersService.addOrder(order);
        return order;
    }

    /**
     * 随机姓氏
     *
     * @return java.lang.String
     */
    public String randFamilyName() {
        String str = "";
        int strLen;
        int randNum = new Random().nextInt(2) + 1;
        int index;
        if (randNum == 1) {
            strLen = familyName.length();
            index = new Random().nextInt(strLen);
            str = String.valueOf(familyName.charAt(index));
        } else {
            strLen = firstName2.length();
            index = new Random().nextInt(strLen);
            if (index % 2 == 0) {
                str = firstName2.substring(index, index + 2);
            } else {
                str = firstName2.substring(index - 1, index + 1);
            }
        }
        return str;
    }

    /**
     * 随机性别
     *
     * @return java.lang.String
     */
    public String randSex() {
        int randNum = new Random().nextInt(2) + 1;
        return randNum == 1 ? "男" : "女";
    }

    /**
     * 随机名字（根据性别）
     *
     * @param sex 性别
     * @return java.lang.String
     */
    public String randName(String sex) {
        String name = "";
        int randNum = new Random().nextInt(2) + 1;
        int index;
        if (sex.equals("男")) {
            int strLen = boyName.length();
            if (randNum % 2 == 0) {
                index = new Random().nextInt(strLen - 1);
                name = boyName.substring(index, index + randNum);
            } else {
                index = new Random().nextInt(strLen);
                name = boyName.substring(index, index + randNum);
            }
        } else {
            int strLen = girlName.length();
            if (randNum % 2 == 0) {
                index = new Random().nextInt(strLen - 1);
                name = girlName.substring(index, index + randNum);
            } else {
                index = new Random().nextInt(strLen);
                name = girlName.substring(index, index + randNum);
            }
        }
        return name;
    }

    /**
     * 随机电话号码
     *
     * @return java.lang.String
     */
    public String getPhoneNum() {
        //给予真实的初始号段，号段是在百度上面查找的真实号段
        String[] start = {"133", "149", "153", "173", "177",

                "180", "181", "189", "199", "130", "131", "132",

                "145", "155", "156", "166", "171", "175", "176", "185", "186", "166", "134", "135",

                "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172",

                "178", "182", "183", "184", "187", "188", "198", "170", "171"};

        //随机出真实号段 使用数组的length属性，获得数组长度，
        //通过Math.random()*数组长度获得数组下标，从而随机出前三位的号段
        String phoneFirstNum = start[(int) (Math.random() * start.length)];
        //随机出剩下的8位数
        String phoneLastNum = "";
        //定义尾号，尾号是8位
        final int LENPHONE = 8;
        //循环剩下的位数
        for (int i = 0; i < LENPHONE; i++) {
            //每次循环都从0~9挑选一个随机数
            phoneLastNum += (int) (Math.random() * 10);

        }
        //最终将号段和尾数连接起来
        return phoneFirstNum + phoneLastNum;
    }

    /**
     * 随机物品
     *
     * @return java.lang.String
     */
    public String getProduct() {
        Random random = new Random();
        int i = random.nextInt(product.length);
        return product[i];
    }

    /**
     * 随机生成价格
     *
     * @param weight 质量
     * @return java.math.BigDecimal
     */
    public BigDecimal getPrice(int weight) {
        Rule rule=ruleService.selectRule();
        Random random = new Random();
        int price = 0;
        if (weight <= rule.getWeightOne()) {
            price = rule.getPriceOne();
        } else if (weight <= rule.getWeightTwo()) {
            price = random.nextInt(rule.getPriceTwoBound()) + rule.getPriceTwoPrice();
        } else if (weight <= rule.getWeightThree()) {
            price = random.nextInt(rule.getPriceThrBound()) + rule.getPriceThrPrice();
        } else {
            price = random.nextInt(rule.getPriceThrBound()*2)+rule.getPriceThrBound()+rule.getPriceThrPrice();
        }
        return new BigDecimal(price);
    }

    /**
     * 随机生成时间
     * @return java.util.Date
     */
    public Date getDate() {
//        Random   rand   =   new   Random();
//        Calendar   cal   =   Calendar.getInstance();
//        cal.set(2019,   0,   1);
//        long   start   =   cal.getTimeInMillis();
//        cal.set(2021,   0,   1);
//        long   end   =   cal.getTimeInMillis();
//        Date   d   =   new   Date(start   +   (long)(rand.nextDouble()   *   (end   -   start)));
        return new Date();
    }
}

